package com.eaoa.uc.busorder.bo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.eaoa.base.EABaseBusinessObject;
import com.eaoa.constants.BusOrderConstants;
import com.eaoa.constants.BusTransferConstants;
import com.eaoa.constants.PubBillConstants;
import com.eaoa.dao.bus.BusOrderDAO;
import com.eaoa.vo.bus.BusCargo;
import com.eaoa.vo.bus.BusOrder;
import com.stony.core.keygenerator.FillCodeBO;

public class BusOrderModifyBO extends EABaseBusinessObject{
	
	private static BusOrderDAO orderDAO = new BusOrderDAO();
	private BusOrder vo ;
	private Long orderno;
	private List busCargos;
	
	public BusOrderModifyBO(BusOrder vo, List busCargos) {
		super();
		this.vo = vo;
		this.busCargos=busCargos;
	}

	protected void performBusinessLogic() {
		orderno = vo.getOrderno();
		
		//calculate summary of number , weight , size 
		BigDecimal numbersum = new BigDecimal(0);
		BigDecimal weightsum = new BigDecimal(0);
		BigDecimal sizesum = new BigDecimal(0);
		for(int i=0; i<busCargos.size(); i++){
			BusCargo cargo = (BusCargo)busCargos.get(i);
			numbersum = numbersum.add(new BigDecimal(cargo.getNumber()));
			weightsum = weightsum.add(cargo.getWeight());
			sizesum = sizesum.add(cargo.getSize());
			vo.setNumbersum(numbersum);
			vo.setWeightsum(weightsum);
			vo.setSizesum(sizesum);
		}
		
		if(orderno==null){
			String billcode = getOrderCode(vo.getOrdertype());
			vo.setBillcode(billcode);
			vo.setBillstatus(BusTransferConstants.ORDER_STATUS_NEW);
			vo.setIstransfer("N");
			vo.setIslock("N");			
			vo.setCreatorno(getEmployee().getAccountno());
			vo.setCreatorname(getEmployee().getName());
			vo.setCreatdate(new Date());
			
			orderno = orderDAO.save(vo);
		}else{
			BusOrder po =  (BusOrder)orderDAO.loadAndLock(orderno);

			//po.setBillcode(vo.getBillcode());
			po.setOrdertype(vo.getOrdertype());
			po.setFromaddressno(vo.getFromaddressno());
			po.setFromaddressname(vo.getFromaddressname());
			po.setFromaddressdetail(vo.getFromaddressdetail());
			po.setFromaddressprovince(vo.getFromaddressprovince());
			po.setFromaddresscity(vo.getFromaddresscity());
			po.setFromaddressdistrict(vo.getFromaddressdistrict());
			po.setFromdate(vo.getFromdate());
			po.setFromman(vo.getFromman());
			po.setFrommanphone(vo.getFrommanphone());
			
			po.setToaddressno(vo.getToaddressno());
			po.setToaddressname(vo.getToaddressname());
			po.setToaddressdetail(vo.getToaddressdetail());
			po.setToaddressprovince(vo.getToaddressprovince());
			po.setToaddresscity(vo.getToaddresscity());
			po.setToaddressdistrict(vo.getToaddressdistrict());
			po.setTodate(vo.getTodate());
			po.setToman(vo.getToman());
			po.setTomanphone(vo.getTomanphone());
			
			po.setTolastdate(vo.getTolastdate());
			po.setTostorebillcode(vo.getTostorebillcode());
			po.setTopickupisself(vo.getTopickupisself());
			po.setProxycustomleter(vo.getProxycustomleter());
			po.setPaymethod(vo.getPaymethod());
			po.setIsinsure(vo.getIsinsure());
			po.setGoodsvalue(vo.getGoodsvalue());
			po.setRemarks(vo.getRemarks());
			
			po.setBusTransfer(vo.getBusTransfer());
			po.setPubCustomer(vo.getPubCustomer());
			
			po.setNumbersum(vo.getNumbersum());
			po.setWeightsum(vo.getWeightsum());
			po.setSizesum(vo.getSizesum());
			
			orderDAO.update(po);
			
		}
		
		//add cargos
		vo.setOrderno(orderno);
		for(int i=0; i<busCargos.size(); i++){
			BusCargo cargo = (BusCargo)busCargos.get(i);
			cargo.setBusOrder(vo);
		}
		new BusCargoModifyBO(busCargos).execute();
		
	}
	
	public Long getOrderno(){
		return orderno;
	}
	
	private String getOrderCode(String ordertype){
		String result ="";
		if(ordertype == null){
			return result;
		}
		if(ordertype.equals(BusOrderConstants.TYPE_THRC)){
			FillCodeBO codeBO = new FillCodeBO(PubBillConstants.BILLCODE_PD_E);
			codeBO.execute();
			result = codeBO.getNewCode();
		}else if(ordertype.equals(BusOrderConstants.TYPE_LDYS)){
			FillCodeBO codeBO = new FillCodeBO(PubBillConstants.BILLCODE_PD_L);
			codeBO.execute();
			result = codeBO.getNewCode();
		}else if(ordertype.equals(BusOrderConstants.TYPE_ZCYS)){
			FillCodeBO codeBO = new FillCodeBO(PubBillConstants.BILLCODE_PD_T);
			codeBO.execute();
			result = codeBO.getNewCode();
		}
		return result;
	}
}